﻿using System;
using System.Drawing;
using System.Windows.Forms;
using System.IO;
using XFControls.Utils;

namespace XFControls.XFPanels.XFPanelItems
{
    public class XFItemImageDisplay : XFItemBase
    {
        public event EventHandler ImageClosed;

        private Rectangle _rec;
        private Bitmap image;
        private Rectangle _closeRec;
        private bool _close;

        public XFItemImageDisplay()
        {
            Style = new XFItemStyle
            {
                PaddingTop = 20,
                PaddingBottom = 20

            };

            _close = false;
            ItemType = XFItemType.ClickableNonChange;
        }

        public override int GetHeight()
        {
            if (_rec.Height == 0)
                return 0;

            int height = _rec.Height + Style.PaddingTop + Style.PaddingBottom;

            if (Buffer == null)
                Buffer = XFBitmapFactory.GetBitmap(GetWidth(false), height);
            return height;
        }

        public override void ItemPaint(Graphics g, int x, int y)
        {
            if (image == null) return;
            g.DrawImage(image, _rec, new Rectangle(0, 0, image.Width, image.Height), GraphicsUnit.Pixel);
            DrawUI("close", g, _closeRec);

            image.Dispose();
            image = null;
        }

        public override XFItemClickResult GetClickResult()
        {
            if (_close)
            {
                _rec = new Rectangle();
                _closeRec = new Rectangle();

                if (ImageClosed != null)
                    ImageClosed(this, new EventArgs());
            }

            return base.GetClickResult();
        }

        public override void OnMouseDown(int x, int y)
        {
            base.OnMouseDown(x, y);

            _close = _closeRec.Contains(x, y);
        }

        public override void Dispose()
        {
            base.Dispose();
            if (image != null)
                image.Dispose();
            image = null;
        }

        public bool LoadImage(string file)
        {
            if (File.Exists(file))
            {
                int width = GetWidth();
                try
                {
                    image = XFGraphics.CreateThumbnailFromFile(file, width, width);
                }
                catch (ArgumentException)
                {
                    MessageBox.Show("Please select an image file");
                    return false;
                }

                Rectangle fit = new Rectangle(0, 0, width, Screen.PrimaryScreen.WorkingArea.Height / 2);
                _rec = new Rectangle(0, Style.PaddingTop, image.Width, image.Height);
                _rec = fit.ScaleToFitInside(_rec, true);

                var tempRec = this.GetImageRec("close");
                _closeRec = new Rectangle(_rec.Width + _rec.X - tempRec.Width, Style.PaddingTop, tempRec.Width, tempRec.Height);

                Invalidate();

                Parent.Reset();
                return true;
            }
            return false;
        }

        public void ClearImage()
        {
            if (image != null)
                image.Dispose();
            image = null;
            _rec = new Rectangle();
            _closeRec = new Rectangle();

            if (ImageClosed != null)
                ImageClosed(this, new EventArgs());
        }
    }
}
